layui.use(['admin'], function () {
    let $ = layui.$
        , admin = layui.admin
        , setter = layui.setter
        , form = layui.form;

    form.on('switch(statusSwitch)', function(data){
        let status = 0;
        if(data.elem.checked){
            status = 1;
        }
        form.val("layPopup", {
            "status": status
        });
    });

    let layerIndex = '';
    layui.data.sendParams = function (params) {
        layerIndex = params.lay_index;
    }

    let field = {};
    let xmSelect1 = '';
    let xmSelect2 = '';
    // 获取数据
    admin.req({
        url: setter.request.domainName + '/admin/security.dataRecycle/add'
        , data: field
        , type: 'get'
        , loading: true // 是否开启加载框
        , done: function (res) {
            let tables = res.data.tables;
            let controllers = res.data.controllers;
            if(tables.toString() === '[object Object]'){
                let selectData = [];
                let initData = [];
                let data_table = form.val("layPopup").data_table;
                if(data_table){
                    initData = [data_table];
                }
                for(let key in tables){
                    let temp = [];
                    temp['name'] = tables[key];
                    temp['id'] = key;
                    selectData.push(temp);
                }
                xmSelect1 = initXmSelect("#xmSelectTable", selectData, initData, 1);
            }
            if(controllers.toString() === '[object Object]'){
                let selectData = [];
                let initData = [];
                let controller = form.val("layPopup").controller;
                if(controller){
                    initData = [controller];
                }
                for(let k in controllers){
                    let temp = [];
                    temp['name'] = controllers[k];
                    temp['id'] = k;
                    selectData.push(temp);
                }
                xmSelect2 = initXmSelect("#xmSelectController", selectData, initData, 2);
            }
        }
    })

    // 新增提交
    form.on('submit(laySubmitAdd)', function(data){
        let field = data.field; //获取提交的字段
        // 提交 Ajax 成功后，关闭当前弹层并重载表格
        admin.req({
            url: setter.request.domainName + '/admin/security.dataRecycle/add' //实际使用请改成服务端真实接口
            , data: field
            , type: 'POST'
            , loading: true // 是否开启加载框
            , done: function(res){
                layui.table.reload('seDRTable'); //重载表格
                admin.message({
                    title: '提交成功'
                    ,offset: '15px'
                    ,icon: 1
                    ,time: 1500
                })
                layer.close(layerIndex); //执行关闭
            }
        })
    });

    // 编辑提交
    form.on('submit(laySubmitEdit)', function(data){
        let field = data.field; //获取提交的字段
        // 提交 Ajax 成功后，关闭当前弹层并重载表格
        admin.req({
            url: setter.request.domainName + '/admin/security.dataRecycle/edit' //实际使用请改成服务端真实接口
            , data: field
            , type: 'POST'
            , loading: true // 是否开启加载框
            , done: function(res){
                layui.table.reload('seDRTable'); //重载表格
                admin.message({
                    title: '提交成功'
                    ,offset: '15px'
                    ,icon: 1
                    ,time: 1500
                })
                layer.close(layerIndex); //执行关闭
            }
        })
    });

    window.initXmSelect = function(ele, selectData = [], initData = [], flag = 1){
        // 下拉选择框
        return xmSelect.render({
            el: ele,
            prop: {
                name: 'name',
                value: 'id',
            },
            language: 'zn',
            autoRow: true, // 自动换行
            filterable: true, // 开启搜索模式
            paging: true, // 开启分页
            pageSize: 50, // 每页显示条数
            initValue: initData, // 默认选中
            radio: true, // 开启单选
            clickClose: true, // 单选完关闭下拉
            toolbar: {
                show: false, // 显示工具条
                list: [ 'ALL', 'CLEAR', 'REVERSE' ], // 全选、清空、反选
            },
            data: selectData,
            on: function(data){
                //arr:  当前多选已选中的数据
                let arr = data.arr;
                //change, 此次选择变化的数据,数组
                let change = data.change;
                //isAdd, 此次操作是新增还是删除
                let isAdd = data.isAdd;

                if(flag === 1){
                    if(isAdd) {
                        let tableName = arr[0]['id'];
                        form.val("layPopup", {
                            "data_table": tableName,
                        });
                        getTablePk(tableName);
                    }else{
                        form.val("layPopup", {
                            "data_table": '',
                            "primary_key": ''
                        });
                    }
                }else if(flag === 2){
                    if(isAdd) {
                        let controller = arr[0]['id'];
                        form.val("layPopup", {
                            "controller": controller
                        });
                    }else{
                        form.val("layPopup", {
                            "controller": '',
                        });
                    }
                }
                // setTimeout(function(){   //穿的两个参数，一个是你要执行的代码块，一个是相隔多少秒
                //     let tableName = xmSelect1.getValue("valueStr");
                //     let controller = xmSelect2.getValue("valueStr");
                //     if(tableName && isAdd){
                //         getTablePk(tableName);
                //     }
                //     if(isAdd) {
                //         form.val("layPopup", {
                //             "data_table": tableName,
                //             "controller": controller
                //         });
                //     }else{
                //         form.val("layPopup", {
                //             "data_table": '',
                //             "controller": '',
                //             "primary_key": ''
                //         });
                //     }
                // },1)
            }
        })
    }

    window.getTablePk = function(tableName){
        // 获取数据
        admin.req({
            url: setter.request.domainName + '/admin/ajax/getTablePk'
            , data: {'table': tableName}
            , type: 'get'
            , loading: true // 是否开启加载框
            , done: function (res) {
                form.val("layPopup", {
                    "primary_key": res.data.pk
                });
            }
        })
    }

})
